package com.jiangyg.mall.ware.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.ware.dto.WareSkuDTO;
import com.jiangyg.mall.ware.service.WareSkuService;
import com.jiangyg.mall.ware.vo.WareSkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：商品库存控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:41:16
 */
@RestController
@RequestMapping("/ware/waresku")
public class WareSkuController {

    /**
     * 商品库存接口
     */
    private final WareSkuService wareSkuService;

    @Autowired
    public WareSkuController(WareSkuService wareSkuService) {
        this.wareSkuService = wareSkuService;
    }

    /**
     * 功能描述：商品库存分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页商品库存
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<WareSkuVO>> select(HttpServletRequest request, @RequestBody Pagination<WareSkuVO> pagination) throws Exception {
        return Result.ok(wareSkuService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改商品库存
     *
     * @param request 请求
     * @param dto     商品库存
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated WareSkuDTO dto) throws Exception {
        return Result.saveOrUpdate(wareSkuService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取商品库存
     *
     * @param request   请求
     * @param wareSkuId 商品库存ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<WareSkuVO> info(HttpServletRequest request, @RequestParam(name = "wareSkuId") Long wareSkuId) throws Exception {
        return Result.ok(wareSkuService.selectById(wareSkuId));
    }

    /**
     * 功能描述：删除商品库存
     *
     * @param request    请求
     * @param wareSkuIds 批量删除的商品库存ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> wareSkuIds) {
        return Result.delete(wareSkuService.delete(wareSkuIds));
    }

}
